package com.xh.fma.commodity.service.impl;


import com.xh.fma.commodity.dao.AttrAttrgroupRelationDao;
import com.xh.fma.commodity.dao.AttrDao;
import com.xh.fma.commodity.entity.AttrAttrgroupRelationEntity;
import com.xh.fma.commodity.entity.AttrEntity;
import com.xh.fma.commodity.service.AttrService;
import com.xh.fma.commodity.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.common.utils.PageUtils;
import com.xh.common.utils.Query;

import com.xh.fma.commodity.dao.AttrgroupDao;
import com.xh.fma.commodity.entity.AttrgroupEntity;
import com.xh.fma.commodity.service.AttrgroupService;

import javax.annotation.Resource;


@Service("attrgroupService")
public class AttrgroupServiceImpl extends ServiceImpl<AttrgroupDao, AttrgroupEntity> implements AttrgroupService {


    @Resource
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Resource
    private AttrDao attrDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                new QueryWrapper<AttrgroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByIdNameCategory(Map<String, Object> params, Long categoryId) {
        //首先通过map拿到查询目标
        String key = (String) params.get("key");
        //得到查询器  并设置查询条件
        QueryWrapper<AttrgroupEntity> wrapper = new QueryWrapper<>();

        //wrapper.eq("id",key).or().like("name",key);
        //使用stream流优化查询逻辑
        if (StringUtils.isNotEmpty(key)){
            wrapper.and((obj) -> {
                obj.eq("id",key).or().like("name",key);
            });
        }

        if (categoryId == 0){
            IPage<AttrgroupEntity> page = this.page(new Query<AttrgroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }

        wrapper.eq("category_id",categoryId);
        IPage<AttrgroupEntity> page = this.page(new Query<AttrgroupEntity>().getPage(params),
                wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getRelationAttr(Long attrGroupId) {

        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities =
                attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));

        if (attrAttrgroupRelationEntities == null || attrAttrgroupRelationEntities.size() == 0 ){
            return null;
        }

        return attrAttrgroupRelationEntities.stream().map(entity -> {
            return attrDao.selectById(entity.getAttrId());
        }).collect(Collectors.toList());

    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupAttr(Long categoryId) {

        // 通过商品id 拿到所有属性分组的集合
        List<AttrgroupEntity> attrgroupEntities =
                this.baseMapper.selectList(new QueryWrapper<AttrgroupEntity>().eq("category_id", categoryId));

        // 遍历各个属性分组
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrgroupEntities.stream().map(item -> {
            //将该属性分组copy到vo中
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);

            //List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities =
            //        attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", item.getId()));
            //List<Long> collect = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            //List<AttrEntity> attrEntities = attrDao.selectBatchIds(collect);
            List<AttrEntity> attrEntities = getRelationAttr(item.getId());
            attrGroupWithAttrsVo.setAttrs(attrEntities);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        System.out.println("master");
        return attrGroupWithAttrsVos;
    }

}